package com.bodacn.andontv;


import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class OneFragment extends Fragment implements View.OnClickListener {
    // TODO: Rename parameter arguments, choose names that match
    public static final int mstRefresh = 1001;
    public static final int msgGetImg = 1002;
    public static final int msgShowImage =1003;
    public static final int msgGetDataOver=1004;

    public static String TAG=OneFragment.class.getName();
    private TextView tv_Depart,tv_StyleNo,tv_PeopleNum,tv_StyleSort,tv_CustName,tv_ManuState,tv_Rank,tv_OnLinePeople,tv_WIP,
            tv_TargetNum,tv_RealNum,tv_TargetEffic,tv_RealEffic,tv_DefectNum,tv_DefectRate,tv_RealRate;
    private ProgressBar prog_WIP, prog_Real,prog_Effic, prog_Defect;
    private TextView tv_UpdateTime,tv_Notice;
    private TextView tv_WIPState,tv_EfficState, tv_DefectState;
    ImageView img_Style;
    private static String RunState="TRUE";
    private static int ThisFragmentIndex=0;
    //用于与主Activity通讯;
    private Handler mainHandler;
    private ProgressDialog waitDialog = null;
    //初始化定时器;
    private Timer timer;
    private TimerTask timetask;
    private static int runCount=0;//计数器，超过一定的次数，就刷新数据;
    //分页显示;
    private JSONArray myJSONArray;//用于存储WebService返回的结果;
    private int totalItemCount=0,pageCount=0,curPage=0;
    private static final int pageItemCount = 1;//每页显示内容;
    String curImageFileName;

    public OneFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建对话框，
        waitDialog=new ProgressDialog(getContext());
        waitDialog.setTitle("请稍等");
        waitDialog.setIndeterminate(true);
        //得到Activity的Handler,用于发消息给主Activity;
        MainActivity activity=(MainActivity) getActivity();
        mainHandler=activity.mainHandler;
        //加接口监听
        activity.setTransOneParamsListener(new MainActivity.OnTransOneParamsListener() {
            @Override
            public void TransParams(Map<String, Object> mParams) {
                if (mParams!=null) {
                    try {
                        Log.d("MSG","setTransOneParamsListener----TransParams--1");
                        String paramComm= mParams.get("ParamComm").toString();
                        //根据参数显示订单信息;
                        if (paramComm=="ParamValue")
                        {
                            Log.d("Main->One","ParamValue------------------>1");
                            //传递登陆参数信息;
                            RunState=mParams.get("RunState").toString();
                            int FragmentIndex=Integer.parseInt(mParams.get("FragmentIndex").toString());
                            //判断是否要停掉刷新;
                            if ((RunState=="TRUE")&&(FragmentIndex==ThisFragmentIndex)) {
                                System.out.println("刷新看板One--------------->Start one");
                            } else{
                                System.out.println("停掉看板One--------------->Over one");
                            }
                            ReGetData();
                            Log.d("Main->One","ParamValue------------------>2");
                        }
                        if (paramComm=="Refresh")
                        {
                            Log.d("Main->One","ParamValue------------------>1");
                            //刷新数据;
                            ReGetData();
                            Log.d("Main->One","ParamValue------------------>2");
                        }

                        Log.d("MSG","setTransOneParamsListener----TransParams--2");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    //启动定时器;
    private void startTimer(){
        int spaceMiSec=5000;
        System.out.println("refreshSpaceSec:"+spaceMiSec);
        if (timer == null) {
            timer = new Timer();
        }
        //定时任务;
        if (timetask == null) {
            timetask = new TimerTask() {
                @Override
                public void run() {
                    //也可发消息，由Handler负责统一调度执行;
                    Message message = new Message();
                    message.what = mstRefresh;
                    handler.sendMessage(message);
                }
            };
            timer.schedule(timetask, 500, spaceMiSec);
        }
        System.out.println(TAG+"startTimer---------------------------------Start");
    }

    /**
     * @param @param  url 本地路径
     * @param @return
     * @return Bitmap
     * @throws
     * @Title: getLoacalBitmap
     * @Description: 加载本地图片
     */
    public Bitmap getLoacalBitmap(String url) {
        if (url != null) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(url);
                return BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return null;
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    fis = null;
                }
            }
        } else {
            return null;
        }
    }


    /**
     * 保存文件
     */
    public void saveBitmap(Bitmap bm, String fileName) {
        //获取MainActivity
        MainActivity activity = (MainActivity) getActivity();
        //String imgfilepath=activity.SaveFilePath+activity.SaveIMGFolder;
        String imgfilepath = activity.getCacheDir().toString();
        Log.d("saveBitmap", "imgfilepath:" + imgfilepath);
        File f = new File(imgfilepath, fileName);
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            if (fileName.toUpperCase().indexOf("PNG") > 0)
                bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            if ((fileName.toUpperCase().indexOf("JPG") > 0) || (fileName.toUpperCase().indexOf("JPEG") > 0))
                bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
            Log.i("saveBitmap", "已经保存成功");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }



    //获取数据;
    private void ReGetData(){
        if (MainApplication.ServerIP.length()>0) {
            String SqlComm = "Exec LookBoardTV @BoardNo='" + MainApplication.BoardNo + "',@OperType='MainScreen'";
            //调用WebService过程并处理界面更新;
            ReadSQLData(SqlComm);
        }
    }

    //通过消息来执行处理类程序,不更新UI则不一定需要通过这个;
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // TODO Auto-generated method stub
            // 根据消息ID来判断要做的事情
            int msgId = msg.what;
            switch (msgId) {
                case mstRefresh:
                    MainActivity activity=(MainActivity) getActivity();
                    if (activity.PushTime=="RUN") {
                        System.out.println("One翻页..........");
                        //处理一些事务;
                        runCount++;
                        curPage++;
                        if (curPage >= pageCount) curPage = 0;
                        if (pageCount>1) PaintScreen(curPage);
                        System.out.println(runCount + "次");
                        if (runCount > activity.switchCount) {
                            runCount = 0;
                        }
                    }
                    break;
                case msgGetDataOver:
                    //输出测试;
                    if (pageCount>0) PaintScreen(curPage);
                    break;
                default:
                    break;
            }
        }
    };

    //刷新缸布列表;
    private void getImage(final String paramStyleNo)
    {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try {
                    ResultSet re = DBUtil.OpenSQL("Exec LookBoardTV_GetStylePNG @StyleNo='" + paramStyleNo + "'");
                    if (re != null) {
                        re.next();
                        //使用Blob对象接收数据库里的图片
                        if (re.findColumn("ImgResult") > -1) {
                            String ImgResult = re.getString("ImgResult");
                            if (ImgResult.equals("OK")) {
                                java.sql.Blob imgBlob = re.getBlob("ImgFile");
                                //获取Blob对象的二进制流;
                                InputStream imgInS = imgBlob.getBinaryStream();
                                //加载一个网络图片
                                Bitmap bitmap = BitmapFactory.decodeStream(imgInS);
                                //保存图片;
                                saveBitmap(bitmap, curImageFileName);
                                //发消息显示图片;
                                Message msg = new Message();
                                msg.what = msgShowImage;
                                handler.removeMessages(msg.what);
                                handler.sendMessage(msg);
                            }
                        }
                    }
                    re.getStatement().getConnection().close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(run).start();
    }

    //判断是否为数字
    public static boolean isNumer(String str) {
        Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
    //从字符串转颜色
    private int GetColorValue(String paramColor){
        int PenColor= Color.WHITE;
        String StrPenColor=paramColor;
        if ((StrPenColor.indexOf("#")==0)&&(StrPenColor.length()==7)){
            int testColor=Integer.parseInt(StrPenColor.substring(1),16);
            int red = (testColor & 0xff0000) >> 16;
            int green = (testColor & 0x00ff00) >> 8;
            int blue = (testColor & 0x0000ff);
            PenColor=Color.rgb(red, green, blue);
        }else {
            if (isNumer(StrPenColor)) {
                int testColor = Integer.parseInt(StrPenColor);
                int red = (testColor & 0xff0000) >> 16;
                int green = (testColor & 0x00ff00) >> 8;
                int blue = (testColor & 0x0000ff);
                PenColor = Color.rgb(red, green, blue);
            } else {
                if (StrPenColor.toLowerCase().equals("black") == true)
                    PenColor = Color.BLACK;
                if (StrPenColor.toLowerCase().equals("blue") == true)
                    PenColor = Color.BLUE;
                if (StrPenColor.toLowerCase().equals("yellow") == true)
                    PenColor = Color.YELLOW;
                if (StrPenColor.toLowerCase().equals("red") == true)
                    PenColor = Color.RED;
                if (StrPenColor.toLowerCase().equals("green") == true)
                    PenColor = Color.GREEN;
                if (StrPenColor.toLowerCase().equals("gray") == true)
                    PenColor = Color.LTGRAY;
                if (StrPenColor.toLowerCase().equals("white") == true)
                    PenColor = Color.WHITE;
            }
        }
        return PenColor;
    }

    //直接连获取数据，与Handler配套使用;
    private void ReadSQLData(final String SQLComm) {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try{
                    JSONArray jsonArray  = DBUtil.QuerySQL2JSON(SQLComm);
                    System.out.println(jsonArray);
                    //往界面转数据;
                    if (jsonArray.length()>0)
                    {
                        myJSONArray=jsonArray;
                    }
                    else myJSONArray=new JSONArray("[]");
                    totalItemCount = myJSONArray.length();
                    //页数向上取整;
                    pageCount=totalItemCount;
                    curPage=0;
                    //发消息显示图片;
                    Message msg = new Message();
                    msg.what = msgGetDataOver;
                    handler.removeMessages(msg.what);
                    handler.sendMessage(msg);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        };
        new Thread(run).start();

    }



    //输出屏幕内容
    private void PaintScreen(int curPage) {
        try {
            String tmpColor="0",tmpRealRate="0";
            if (myJSONArray.length()>0) {
                System.out.println("PaintScreen:["+myJSONArray.length()+"]--------------------->"+curPage);
                if (myJSONArray.length()> curPage) {
                    if (myJSONArray.optJSONObject(curPage).has("Depart") == true)
                        tv_Depart.setText(myJSONArray.getJSONObject(curPage).getString("Depart"));
                    if (myJSONArray.optJSONObject(curPage).has("StyleNo") == true)
                        tv_StyleNo.setText(myJSONArray.getJSONObject(curPage).getString("StyleNo"));
                    if (myJSONArray.optJSONObject(curPage).has("PeopleNum") == true)
                        tv_PeopleNum.setText(myJSONArray.getJSONObject(curPage).getString("PeopleNum"));
                    if (myJSONArray.optJSONObject(curPage).has("StyleSort") == true)
                        tv_StyleSort.setText(myJSONArray.getJSONObject(curPage).getString("StyleSort"));
                    if (myJSONArray.optJSONObject(curPage).has("CustName") == true)
                        tv_CustName.setText(myJSONArray.getJSONObject(curPage).getString("CustName"));
                    if (myJSONArray.optJSONObject(curPage).has("ManuState") == true)
                        tv_ManuState.setText(myJSONArray.getJSONObject(curPage).getString("ManuState"));
                    if (myJSONArray.optJSONObject(curPage).has("Rank") == true)
                        tv_Rank.setText(myJSONArray.getJSONObject(curPage).getString("Rank"));
                    if (myJSONArray.optJSONObject(curPage).has("OnLinePeople") == true)
                        tv_OnLinePeople.setText(myJSONArray.getJSONObject(curPage).getString("OnLinePeople"));
                    if (myJSONArray.optJSONObject(curPage).has("WIP") == true)
                        tv_WIP.setText(myJSONArray.getJSONObject(curPage).getString("WIP"));
                    if (myJSONArray.optJSONObject(curPage).has("TargetNum") == true)
                        tv_TargetNum.setText(myJSONArray.getJSONObject(curPage).getString("TargetNum"));
                    if (myJSONArray.optJSONObject(curPage).has("RealNum") == true)
                        tv_RealNum.setText(myJSONArray.getJSONObject(curPage).getString("RealNum"));
                    if (myJSONArray.optJSONObject(curPage).has("TargetEffic") == true)
                        tv_TargetEffic.setText(myJSONArray.getJSONObject(curPage).getString("TargetEffic"));
                    if (myJSONArray.optJSONObject(curPage).has("RealEffic") == true)
                        tv_RealEffic.setText(myJSONArray.getJSONObject(curPage).getString("RealEffic"));
                    if (myJSONArray.optJSONObject(curPage).has("DefectNum") == true)
                        tv_DefectNum.setText(myJSONArray.getJSONObject(curPage).getString("DefectNum"));
                    if (myJSONArray.optJSONObject(curPage).has("DefectRate") == true)
                        tv_DefectRate.setText(myJSONArray.getJSONObject(curPage).getString("DefectRate"));
                    if (myJSONArray.optJSONObject(curPage).has("ManuState") == true)
                        tv_ManuState.setText(myJSONArray.getJSONObject(curPage).getString("ManuState"));
                    if (myJSONArray.optJSONObject(curPage).has("ManuColor") == true) {
                        tv_ManuState.setTextColor(GetColorValue(myJSONArray.optJSONObject(curPage).getString("ManuColor")));
                    }
                    if (myJSONArray.optJSONObject(curPage).has("Rank") == true)
                        tv_Rank.setText(myJSONArray.getJSONObject(curPage).getString("Rank"));
                    if (myJSONArray.optJSONObject(curPage).has("RankColor") == true) {
                        tv_Rank.setTextColor(GetColorValue(myJSONArray.optJSONObject(curPage).getString("RankColor")));
                    }
                    if (myJSONArray.optJSONObject(curPage).has("RealRate") == true) {
                        tmpRealRate=myJSONArray.getJSONObject(curPage).getString("RealRate");
                        tv_RealRate.setText(tmpRealRate+"%");
                        try {
                            if (isNumer(tmpRealRate) == true)
                                prog_Real.setProgress(Integer.valueOf(tmpRealRate));
                        }catch (Exception e){
                            Log.d(TAG, "PaintScreen: "+e.getMessage());
                        }
                    }
                    if (myJSONArray.optJSONObject(curPage).has("ColorReal") == true) {
                        tmpColor=myJSONArray.getJSONObject(curPage).getString("ColorReal");
                        if ((tmpColor.equals("1")==true)||(tmpColor.toLowerCase().equals("red")==true)) prog_Real.setProgressDrawable(getResources().getDrawable(R.drawable.processbar_style_red));
                        if ((tmpColor.equals("2")==true)||(tmpColor.toLowerCase().equals("yellow")==true)) prog_Real.setProgressDrawable(getResources().getDrawable(R.drawable.processbar_style_yellow));
                        if ((tmpColor.equals("3")==true)||(tmpColor.toLowerCase().equals("green")==true)) prog_Real.setProgressDrawable(getResources().getDrawable(R.drawable.processbar_style_green));
                        tv_RealNum.setTextColor(GetColorValue(tmpColor));
                    }
                    if (myJSONArray.optJSONObject(curPage).has("ColorWIP") == true) {
                        tmpColor=myJSONArray.getJSONObject(curPage).getString("ColorWIP");
                        if ((tmpColor.equals("1")==true)||(tmpColor.toLowerCase().equals("red")==true))   prog_WIP.setProgressDrawable(getResources().getDrawable(R.drawable.processbar_style_red));
                        if ((tmpColor.equals("2")==true)||(tmpColor.toLowerCase().equals("yellow")==true)) prog_WIP.setProgressDrawable(getResources().getDrawable(R.drawable.processbar_style_yellow));
                        if ((tmpColor.equals("3")==true)||(tmpColor.toLowerCase().equals("green")==true)) prog_WIP.setProgressDrawable(getResources().getDrawable(R.drawable.processbar_style_green));
                        tv_WIP.setTextColor(GetColorValue(tmpColor));
                    }
                    if (myJSONArray.optJSONObject(curPage).has("ColorEffic") == true) {
                        tmpColor=myJSONArray.getJSONObject(curPage).getString("ColorEffic");
                        if ((tmpColor.equals("1")==true)||(tmpColor.toLowerCase().equals("red")==true)) prog_Effic.setProgressDrawable(getResources().getDrawable(R.drawable.processbar_style_red));
                        if ((tmpColor.equals("2")==true)||(tmpColor.toLowerCase().equals("yellow")==true)) prog_Effic.setProgressDrawable(getResources().getDrawable(R.drawable.processbar_style_yellow));
                        if ((tmpColor.equals("3")==true)||(tmpColor.toLowerCase().equals("green")==true)) prog_Effic.setProgressDrawable(getResources().getDrawable(R.drawable.processbar_style_green));
                        tv_RealEffic.setTextColor(GetColorValue(tmpColor));
                    }
                    if (myJSONArray.optJSONObject(curPage).has("ColorDefect") == true) {
                        tmpColor=myJSONArray.getJSONObject(curPage).getString("ColorDefect");
                        if ((tmpColor.equals("1")==true)||(tmpColor.toLowerCase().equals("red")==true)) prog_Defect.setProgressDrawable(getResources().getDrawable(R.drawable.processbar_style_red));
                        if ((tmpColor.equals("2")==true)||(tmpColor.toLowerCase().equals("yellow")==true)) prog_Defect.setProgressDrawable(getResources().getDrawable(R.drawable.processbar_style_yellow));
                        if ((tmpColor.equals("3")==true)||(tmpColor.toLowerCase().equals("green")==true)) prog_Defect.setProgressDrawable(getResources().getDrawable(R.drawable.processbar_style_green));
                        tv_DefectRate.setTextColor(GetColorValue(tmpColor));
                    }
                    if (myJSONArray.optJSONObject(curPage).has("WIPState") == true) tv_WIPState.setText(myJSONArray.optJSONObject(curPage).getString("WIPState"));
                    if (myJSONArray.optJSONObject(curPage).has("EfficState") == true) tv_EfficState.setText(myJSONArray.optJSONObject(curPage).getString("EfficState"));
                    if (myJSONArray.optJSONObject(curPage).has("DefectState") == true) tv_DefectState.setText(myJSONArray.optJSONObject(curPage).getString("DefectState"));
                    //判断是否有图片
                    if (myJSONArray.getJSONObject(curPage).has("ImgFileName") == true){
                        curImageFileName =myJSONArray.getJSONObject(curPage).getString("ImgFileName");
                        //判断本地是否有图片;
                        String imgfilepath = getActivity().getCacheDir().toString();
                        File file = new File(imgfilepath + "/" + curImageFileName);
                        System.out.println("图片文件路径:" + imgfilepath + "/" + curImageFileName);
                        //判断图片是否存在，如果不存在则从网络获取;
                        if (file.exists() == true) {
                            Log.d(TAG, "准备从本地取图片显示---------------");
                            //从本地取图片
                            Bitmap bitmap = getLoacalBitmap(imgfilepath + "/" + curImageFileName);
                            img_Style.setImageBitmap(bitmap);
                        } else {
                            Log.d(TAG, "发消息从网络获取图片---------------");
                            Drawable drawable = getResources().getDrawable(R.mipmap.err404);
                            BitmapDrawable errBitDraw = (BitmapDrawable) drawable;
                            img_Style.setImageBitmap(errBitDraw.getBitmap());
                            //发前消息，获取图片;
                            Message msgImg = new Message();
                            msgImg.what = msgGetImg;
                            handler.removeMessages(msgImg.what);
                            handler.sendMessage(msgImg);
                        }
                    }
                    //通知及更新时间
                    if (myJSONArray.getJSONObject(curPage).has("UpdateTime") == true){
                        tv_UpdateTime.setText(myJSONArray.getJSONObject(curPage).getString("UpdateTime"));
                    }
                    if (myJSONArray.getJSONObject(curPage).has("Notice") == true){
                        if (tv_Notice.getText()!=null) {
                            if (tv_Notice.getText().toString().equals(myJSONArray.getJSONObject(curPage).getString("Notice")) == false)
                                tv_Notice.setText(myJSONArray.getJSONObject(curPage).getString("Notice"));
                        } else tv_Notice.setText(myJSONArray.getJSONObject(curPage).getString("Notice"));

                    }
                    if (myJSONArray.getJSONObject(curPage).has("NoticeColor") == true){
                        tv_Notice.setTextColor(GetColorValue(myJSONArray.optJSONObject(curPage).getString("NoticeColor")));
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }



    @Override
    public void onClick(View v) {

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_one, container, false);
        InitView(root);//按钮初始化;
        System.out.println("OneFragment_onCreateView");
        return root;
    }

    //界面初始化;
    private void InitView(View v) {
        //界面控件;
        tv_Depart=v.findViewById(R.id.tv_Depart);
        tv_StyleNo=v.findViewById(R.id.tv_StyleNo);
        tv_PeopleNum=v.findViewById(R.id.tv_PeopleNum);
        tv_StyleSort=v.findViewById(R.id.tv_StyleSort);
        tv_ManuState=v.findViewById(R.id.tv_ManuState);
        tv_Rank=v.findViewById(R.id.tv_Rank);
        tv_OnLinePeople=v.findViewById(R.id.tv_OnLinePeople);
        tv_WIP=v.findViewById(R.id.tv_WIP);
        tv_TargetNum=v.findViewById(R.id.tv_TargetNum);
        tv_RealNum=v.findViewById(R.id.tv_RealNum);
        tv_TargetEffic=v.findViewById(R.id.tv_TargetEffic);
        tv_RealEffic=v.findViewById(R.id.tv_RealEffic);
        tv_DefectNum=v.findViewById(R.id.tv_DefectNum);
        tv_DefectRate=v.findViewById(R.id.tv_DefectRate);
        tv_RealRate=v.findViewById(R.id.tv_RealRate);
        tv_UpdateTime=v.findViewById(R.id.tv_UpdateTime);
        tv_Notice=v.findViewById(R.id.tv_Notice);
        tv_WIPState=v.findViewById(R.id.tv_WIPState);
        tv_EfficState=v.findViewById(R.id.tv_EfficState);
        tv_DefectState =v.findViewById(R.id.tv_DefectState);
        //进度条相关
        prog_WIP=v.findViewById(R.id.prog_WIP);
        prog_Real =v.findViewById(R.id.prog_Real);
        prog_Effic=v.findViewById(R.id.prog_Effic);
        prog_Defect =v.findViewById(R.id.prog_Defect);
        //图片显示;
        img_Style=v.findViewById(R.id.img_Style);
        try {
            myJSONArray=new JSONArray("[]");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    //停止计时器;
    private void stopTimer(){
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timetask != null) {
            timetask.cancel();
            timetask = null;
        }
        if (waitDialog!=null) waitDialog.dismiss();
        System.out.println(TAG+"startTimer---------------------------------Start");
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        stopTimer();
    }

    @Override
    public void onStart() {
        super.onStart();
        startTimer();
    }

    @Override
    public void onPause() {
        super.onPause();

        System.out.println("OneFragment----onPause");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        System.out.println(TAG+":onHiddenChanged"+hidden);
        if (hidden==false){
            tv_Notice.setFocusable(true);
            tv_Notice.setFocusableInTouchMode(true);
            tv_Notice.requestFocus();
            tv_Notice.requestFocusFromTouch();
        } else{
            tv_Notice.setFocusable(false);
            tv_Notice.setFocusableInTouchMode(false);
        }
    }
}
